package com.jian.utils;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;

public class PackageUtils
{
	public static boolean checkapp(Context c, String PackName)
	{
		PackageManager pM = c.getPackageManager();
		try {
			PackageInfo packageInfo = pM.getPackageInfo(PackName, 0);
			return null != packageInfo;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static void jump(Context c, String PackName)
	{
		PackageManager pM = c.getPackageManager();
		try {

			Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
			resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
			resolveIntent.setPackage(PackName);
			List<ResolveInfo> apps = pM.queryIntentActivities(resolveIntent, 0);
			if (null != apps) {
				ResolveInfo ri = apps.iterator().next();
				String classname = ri.activityInfo.name;
				Intent openapp = new Intent(Intent.ACTION_MAIN);
				openapp.addCategory(Intent.CATEGORY_LAUNCHER);
				ComponentName cn = new ComponentName(PackName, classname);
				openapp.setComponent(cn);
				c.startActivity(openapp);

			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
